package com.ds.server.support;

import org.lwjgl.util.vector.Vector3f;

// the class using to detect collisions among cars and map
public class CollisionCtrl {
	public static final int nOffset = 0;
	public static final int mOffset = 0;

	
	public static boolean collision(ServerCar car, ServerCar other){
		CollisionCtrl.updateLoop(car);
		return carCollision(car, other) || raceCollison(car);
	}
	
	// detect the collision between cars
	public static boolean carCollision(ServerCar car, ServerCar other) {
		if(Math.pow(car.getX() - other.getX(), 2)
				+ Math.pow(car.getY() - other.getY(), 2) <= 1600){
			car.setSpeed(0);
			other.setSpeed(0);
			return true;
		}
		else
			return false;
	}
	
	// detect the collision between car and map
	public static boolean raceCollison(ServerCar car) {
		CollisionCtrl.updateLoop(car);
		System.out.println(car.getX()+" "+car.getY());
		
		float xl=car.getX() - mOffset;
		float xr=car.getX() + mOffset;
		float yt=car.getY() + mOffset;
		float yb=car.getY() - mOffset;
		
		if(xl>=427&&xr<=638&&yt<=1437&&yb>=432||
		   xl>=427&&xr<=2240&&yt<=1437&&yb>=1257||	
		   xl>=2057&&xr<=2240&&yt<=1437&&yb>=432||
		   xl>=427&&xr<=2240&&yt<=611&&yb>=432)
			return false;
		else{
			car.setSpeed(0);
			car.getPos();
			Vector3f currentPos = car.getPos();
				if(xl<=466){
					currentPos.x+=30;
					System.out.println("left: "+currentPos.x);
				}
				if(xr<=1000&&xr>=638&&yt<=1257&&yb>=611){
					currentPos.x=currentPos.x-50;
				}
				car.setX(currentPos.x);

				if(yt>=1437){
					currentPos.y-=30;
				}
				if(yb>=800&&yb<=1257&&xl>=638&&xr<=2057){
					currentPos.y+=50;
				}
				car.setY(currentPos.y);

				if(xl>=1000&&xl<=2057&&yb>=611&&yt<=1257){
					currentPos.x+=50;
				}
				if(xr>=2240){
					currentPos.x-=30;
				}
				car.setX(currentPos.x);

				if(yt<= 1000&&yt>=611&&xl>=638&&xr<=2057){
					currentPos.y-=50;
				}
				if(yb<=432){
					currentPos.y+=30;
				}
				car.setY(currentPos.y);
			return true;
		}
	}

	// detect the collision between car and the starting line
	public static void updateLoop(ServerCar car){
		if(car.getX()<=638&&car.getX()>=427&&car.getY()>=432&&car.getY()<=611){
			car.point1=true;
		}
		if(car.getX()<=638&&car.getX()>=427&&car.getY()>=611&&car.getY()<=1257){
			car.point2=true;
		}
		if(car.getX()<=2057&&car.getX()>=427&&car.getY()>=1257&&car.getY()<=1437&&car.point2){
			car.point3=true;
		}
		if(car.getX()<=2240&&car.getX()>=2057&&car.getY()>=611&&car.getY()<=1437&&car.point3){
			car.point4=true;
		}
		if(car.getX()<=2240&&car.getX()>=2057&&car.getY()>=432&&car.getY()<=611&&car.point4){
			car.point5=true;
		}
		if(car.getX()<=2057&&car.getX()>=638&&car.getY()>=432&&car.getY()<=611&&car.point5){
			car.point6=true;
		}
		if(car.getY()<=795&&car.getY()>715&&car.point1&&car.point2&&car.point3&&car.point4&&car.point5&&car.point6){
			car.setLoop(1);
			car.point1=false;
			car.point2=false;
			car.point3=false;
			car.point4=false;
			car.point5=false;
			car.point6=false;			
		}
	}
	
}
